/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package encomienda.dao;

import encomienda.modelo.Cliente;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
/**
 *
 * @author JUAN
 */
public class ClienteDaoImpl implements ClienteDao{
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("EncomiendaPU"); //Conexion a Base de Datos
    EntityManager manager=factory.createEntityManager();
    EntityTransaction tran=null;
    
    /*--------------------------------------------------------------------------------- */
    
    @Override
    public List<Cliente> reportarCliente(){
    Query qry=manager.createQuery("select c from Cliente c");
    return  qry.getResultList();      
    }
    /*--------------------------------------------------------------------------------- */
    
    @Override
    public void insertarCliente(Cliente to){
    tran=manager.getTransaction();
    tran.begin();
    manager.persist(to);
    tran.commit();
    }
    /*--------------------------------------------------------------------------------- */
    @Override
    public void eliminarCliente(String idCliente){
    tran=manager.getTransaction();
    tran.begin();
    Cliente to=manager.find(Cliente.class, idCliente);
    manager.remove(to);//para eliminar
    tran.commit();
   }
   /*--------------------------------------------------------------------------------- */
    @Override
    public Cliente reportaClienteId(String idcliente){
        Cliente dato= manager.find(Cliente.class, idcliente);
        return dato;
    }
   
    @Override
    public Cliente actualizarCliente(Cliente dato){//void
    tran= manager.getTransaction();
    tran.begin();
    manager.merge(dato);//update
    tran.commit();
    return dato;
    }
   /*--------------------------------------------------------------------------------- */
   @Override
   public List<Cliente> reportarClienteCliente(String cliente){
   EntityManager manager=factory.createEntityManager();
   Query qry=manager.createNativeQuery("select * from Cliente a where a.nombre like ?1 ", Cliente.class)
           .setParameter(1, "%"+cliente+"%");
           
   return  qry.getResultList();       
   } 
    /*--------------------------------------------------------------------------------- */
}
